package com.hzqy.web.ac.epg;

import com.alibaba.fastjson.JSONObject;
import com.hzqy.commons.utils.FileType;
import com.hzqy.service.epg.AcDrawServiceBean;
import com.hzqy.web.vo.CmActivityVo;
import com.hzqy.web.vo.epg.AcDrawButtonVo;
import com.hzqy.web.vo.epg.AcDrawChartVo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import static com.hzqy.commons.utils.FileUtil.fileCheck;

@Controller
@RequestMapping("/epgdraw")
public class AcDrawAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	private static final String[] allowedFileType = new String[]{"png", "jpg", "gif","zip","jar","tar","docx","doc","xlsx","xls","flv","mp4","mp3","mpg","avi","mov","wmv"};
	@Resource
	private AcDrawServiceBean acSevice;

	/***
	 * @param   活动总计统计
	 * @return
	 */
	@RequestMapping(value = "/actionList")
	@ResponseBody
	public Object actionList(CmActivityVo acv,int page, int rows) {
		Map<String, Object> map = acSevice.selectActionList(acv,page,rows);
		return map;
	}
	/***
	 * @param   添加活动记录
	 * @return
	 */
	@RequestMapping(value = "/insertAction")
	@ResponseBody
	public Object insertAction(CmActivityVo acv) {
		Map<String, Object> map = acSevice.insertAction(acv);
		return map;
	}
	/***
	 * @param   修改活动记录
	 * @return
	 */
	@RequestMapping(value = "/updateAction")
	@ResponseBody
	public Object updateAction(CmActivityVo acv) {
		Map<String, Object> map  = acSevice.updateAction(acv);
		return map;
	}
	/***
	 * @param   删除活动记录
	 * @return
	 */
	@RequestMapping(value = "/deleteAction")
	@ResponseBody
	public Object deleteAction(int f_aca_id) {
		Map<String, Object> map  = acSevice.deleteAction(f_aca_id);
		return map;
	}

	/***
	 * @param   根据id查询活动记录
	 * @return
	 */
	@RequestMapping(value = "/seletcActionById")
	@ResponseBody
	public Object seletcActionById(int f_aca_id) {
		CmActivityVo acv  = acSevice.seletcActionById(f_aca_id);
		return acv;
	}

	/**
	 * 活动相关页面图片编辑 以json形式传入相关信息
	 * @param json
	 * @return
	 */

	@RequestMapping(value = "/actionViewUpdate")
	@ResponseBody
	public Object actionViewUpdate(String json) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		LOG.debug("specialViewDataUpdate-json:"+json);
		JSONObject jsonObject=JSONObject.parseObject(json);
		int flag = acSevice.actionViewUpdate(jsonObject);
		if(flag !=-1) {
			resultMap.put("success", true);
			resultMap.put("result","修改活动内容成功!");
		} else {
			resultMap.put("success", true);
			resultMap.put("result","修改活动内容失败!flag="+flag);
		}
		return resultMap;
	}


	/***
	 * @param f_adc_activityid 活动id
	 * 	f_adc_type   类型：1：活动首页，2：中奖列表页；3：活动详情页
	 * @return
	 */
	@RequestMapping(value = "/chartList")
	@ResponseBody
	public Object selectChartList(AcDrawChartVo adcvo) {
		Map<String, Object> map = null;
		map = acSevice.selectChartList(adcvo);
		return map;
	}

	/***
	 * @param f_adb_draw_id   对应页面
	 * @return
	 */
	@RequestMapping(value = "/buttonList")
	@ResponseBody
	public Object selectButtonList(String json) {
		Map<String, Object> map = null;
		JSONObject jsonObject=JSONObject.parseObject(json);
		AcDrawButtonVo adbvo=new AcDrawButtonVo();
		adbvo.setF_adc_type(jsonObject.getString("f_adc_type"));
		adbvo.setF_adc_activityid(jsonObject.getString("f_adc_activityid"));
		map = acSevice.selectButtonList(adbvo);
		return map;
	}
	/**
	 * 上传图片
	 * @param request
	 * @param response
	 * @param session
	 * @throws Exception
	 */
/*	@RequestMapping("/gdacUploadPictures")
	@ResponseBody
	public void activityUploadPictures(HttpServletRequest request ,HttpServletResponse response,HttpSession session) throws Exception {
		String picLeftFile = session.getServletContext().getInitParameter("img_path");
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("uploadify");
		if (FileType.checkFileType(file.getInputStream(), allowedFileType)) {
			String uploadifyFileName = file.getOriginalFilename();
			String picType = request.getParameter("picType");
			picType = URLDecoder.decode(picType, "UTF-8");
			String filePath = acSevice.getFilePath(picType);
			String fileName = acSevice.getFileName(uploadifyFileName);
			if(fileCheck(picLeftFile + filePath, fileName)){
				File picFile = new File(picLeftFile + filePath, fileName);
				if (!picFile.exists())
					picFile.mkdirs();
				try {
					file.transferTo(picFile);
					LOG.debug("上传图片成功！");
				} catch (Exception e) {
					e.printStackTrace();
					LOG.error("上传图片失败！", e);
				}
				FileInputStream resultStream = new FileInputStream(picFile);
				BufferedImage img = ImageIO.read(resultStream);
				response.setContentType("text/html;charset=UTF-8");
				try {
					PrintWriter out = response.getWriter();
					String jsonData = "{\"width\":\"" + img.getWidth() + "\",\"height\":\"" + img.getHeight() + "\",\"imgpath\":\"" + filePath + fileName + "\"}";
					LOG.debug("uploadPicture-filePath+fileName:" + filePath + fileName + "--jsonData:" + jsonData);
					out.write(jsonData);
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					try {
						resultStream.close();
					} catch (IOException e) {
					}
				}
			}
		} else {
			return;
		}
	}*/
}